package com.ruoyi.car.service.impl;

import com.ruoyi.car.domain.CarCommunity;
import com.ruoyi.car.domain.CarCommunityReply;
import com.ruoyi.car.mapper.CarCommunityMapper;
import com.ruoyi.car.service.ICarCommunityReplyService;
import com.ruoyi.car.service.ICarCommunityService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 社区帖子Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class CarCommunityServiceImpl implements ICarCommunityService {
    @Autowired
    private CarCommunityMapper carCommunityMapper;

    @Autowired
    private ICarCommunityReplyService carCommunityReplyService;

    /**
     * 查询社区帖子
     *
     * @param id 社区帖子主键
     * @return 社区帖子
     */
    @Override
    public CarCommunity selectCarCommunityById(Long id) {
        return carCommunityMapper.selectCarCommunityById(id);
    }

    /**
     * 查询社区帖子列表
     *
     * @param carCommunity 社区帖子
     * @return 社区帖子
     */
    @Override
    public List<CarCommunity> selectCarCommunityList(CarCommunity carCommunity) {
        List<CarCommunity> carCommunityList = carCommunityMapper.selectCarCommunityList(carCommunity);
        fillReply(carCommunityList, carCommunity.getNeedReply());
        return carCommunityList;
    }

    private void fillReply(List<CarCommunity> carCommunityList, Boolean needReply) {
        if (CollectionUtils.isEmpty(carCommunityList)) {
            return;
        }
        if (Objects.equals(needReply, true)) {
            for (CarCommunity community : carCommunityList) {
                CarCommunityReply queryReply = new CarCommunityReply();
                queryReply.setCommunityId(community.getId());
                community.setReplyList(carCommunityReplyService.selectCarCommunityReplyList(queryReply));
            }
        }

    }

    /**
     * 新增社区帖子
     *
     * @param carCommunity 社区帖子
     * @return 结果
     */
    @Override
    public int insertCarCommunity(CarCommunity carCommunity) {
        carCommunity.setCreateTime(DateUtils.getNowDate());
        return carCommunityMapper.insertCarCommunity(carCommunity);
    }

    /**
     * 修改社区帖子
     *
     * @param carCommunity 社区帖子
     * @return 结果
     */
    @Override
    public int updateCarCommunity(CarCommunity carCommunity) {
        carCommunity.setUpdateTime(DateUtils.getNowDate());
        return carCommunityMapper.updateCarCommunity(carCommunity);
    }

    /**
     * 批量删除社区帖子
     *
     * @param ids 需要删除的社区帖子主键
     * @return 结果
     */
    @Override
    public int deleteCarCommunityByIds(Long[] ids) {
        return carCommunityMapper.deleteCarCommunityByIds(ids);
    }

    /**
     * 删除社区帖子信息
     *
     * @param id 社区帖子主键
     * @return 结果
     */
    @Override
    public int deleteCarCommunityById(Long id) {
        return carCommunityMapper.deleteCarCommunityById(id);
    }
}
